/*
 *  Copyright 2011 Renato Couto da Costa.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package br.com.renatoccosta.mediabox;

import br.com.renatoccosta.mediabox.data.FileType;
import br.com.renatoccosta.mediabox.data.Media;
import br.com.renatoccosta.mediabox.data.Repository;
import br.com.renatoccosta.mediabox.data.controller.MediaJpaController;
import br.com.renatoccosta.mediabox.data.controller.RepositoryJpaController;
import br.com.renatoccosta.mediabox.util.MediaNameUtils;
import java.io.File;
import java.io.FileFilter;

/**
 *
 * @author Renato Costa
 */
public class RepositoryScanner {

    public static void addRepository(String path) {
        File folder = new File(path);

        File[] files = folder.listFiles(new FileFilter() {

            public boolean accept(File pathname) {
                return pathname.getName().toLowerCase().endsWith("jpg");
            }

        });

        if (files.length > 0) {
            Repository r = new Repository();
            r.setFolderPath(path);

            RepositoryJpaController rjc = new RepositoryJpaController();
            rjc.create(r);

            MediaJpaController mjc = new MediaJpaController();

            for (File file : files) {
                Media m = new Media();
                m.setRepository(r);
                m.setType(FileType.IMAGE);
                m.setName(MediaNameUtils.inferMediaNameFromFileName(
                        file.getName()));

                mjc.create(m);
            }
        }
    }

}
